<?php

function dbconnect($dbhost = 'localhost', $dbuser = 'root', $dbpass = '', $dbname = 'VPMT')
	{
		$link = mysql_pconnect($dbhost,$dbuser,$dbpass) or die("Unable to Connect to Database");
		mysql_select_db($dbname,$link) or die(createdb($dbname, $link));
	}

function createdb($dbname = 'VPMT', $link)
	{
		$sql = "CREATE DATABASE " . $dbname;
		$result = mysql_query($sql) or die("Database Creation Failed");
			mysql_select_db($dbname,$link) or die("Unable to select database");
		$sql = "CREATE TABLE `Phones` (`ID` INT(10) unsigned NOT NULL auto_increment, ";
		$sql .= "`MAC` CHAR(30) default NULL, `Extension` CHAR(20) default NULL, `User` CHAR(40) default NULL, ";
		$sql .= "`Position` CHAR(80) default NULL, `Team` CHAR(80) default NULL, `Location` CHAR(80) default NULL, ";
		$sql .= "`EMail` CHAR(80) default NULL, PRIMARY KEY (`ID`)) TYPE=MyISAM;";
			mysql_query($sql) or die("Table Creation Failed");
		$sql = "CREATE TABLE `Settings` (`ID` INT(10) unsigned NOT NULL auto_increment, ";
		$sql .= "`Phone` CHAR(30) default NULL, `Keyword` CHAR(40) default NULL, `Value` CHAR(80) default NULL, ";
		$sql .= "`Note` TEXT default NULL, PRIMARY KEY (`ID`)) TYPE=MyISAM;";
			mysql_query($sql) or die("Table Creation Failed");
		$sql = "CREATE TABLE `SNOM` (`ID` INT(10) unsigned NOT NULL auto_increment, ";
		$sql .= "`Keyword` CHAR(40) default NULL, `Default` CHAR(80) default NULL, PRIMARY KEY (`ID`)) TYPE=MyISAM;";
			mysql_query($sql) or die("Table Creation Failed");
		$sql = "INSERT INTO Phones VALUES ('','00:00:00:00:00','1001','Timmy','Location','Boss','Management','user@domain.com');";
			mysql_query($sql) or die($sql);
		$sql = "INSERT INTO Settings VALUES ('','1','language','English','Interface Language');";
			mysql_query($sql) or die($sql);
		$sql = "INSERT INTO Settings VALUES ('','1','Setting','Value','testing');";
			mysql_query($sql) or die($sql);
		$sql = "INSERT INTO SNOM VALUES ('','dhcp','on');";
			mysql_query($sql) or die($sql);
		ob_end_clean();
		header("Location: {$_SERVER['PHP_SELF']}");
	}

function addtodb($Table, $DBData)
	{
		dbconnect();
		$dbresult = mysql_query("SELECT * FROM {$Table}");
		$i = mysql_num_fields($dbresult) - 1;
		$sql = ");";
		while(0 < $i--) $sql = ", '{$DBData[$i]}'" . $sql;
		$sql = "INSERT INTO {$Table} VALUES (''" . $sql;
		mysql_query($sql) or die($sql);
		ob_end_clean();
		header("Location: {$_SERVER['PHP_SELF']}");
	}

function deletefromdb($table, $value)
	{
		dbconnect();
		$sql = "DELETE FROM {$table} WHERE ID =" . $value;
		mysql_query ($sql) or die ($sql);
		ob_end_clean();
		header("Location: {$_SERVER['PHP_SELF']}");
	}

function updatedb($Table, $DBData, $ID)
	{
		dbconnect();
		$dbresult = mysql_query("SELECT * FROM $Table");
		$flds = mysql_num_fields($dbresult) - 1;
		while($flds > $i) $fldnames[] = mysql_field_name($dbresult, ++$i);
		$sql = " WHERE ID = '{$ID}'";
		while(0 < $i--)
			{
				$sql = "{$fldnames[$i]} = '{$DBData[$i]}'" . $sql;
				if($i >= 1) $sql = ", " . $sql;
			}
		$sql = "UPDATE {$Table} SET " . $sql;
		mysql_query($sql) or die("Update Failed: $sql");
		ob_end_clean();
		header("Location: {$_SERVER['PHP_SELF']}");
	}

function cycledb($dbhost = 'localhost', $dbuser = 'root', $dbpass = '', $dbname = 'VPMT')
	{
		$link = mysql_pconnect($dbhost,$dbuser,$dbpass) or die("Unable to Connect to Database");
		mysql_select_db($dbname,$link) or die(createdb($dbname, $link));
		$sql = "DROP DATABASE " . $dbname;
		$result = mysql_query($sql) or die("Not able to delete. Check your permisions");
		createdb($dbname, $link);
		ob_end_clean();
		header("Location: {$_SERVER['PHP_SELF']}");
	}

function settingsdd($name = 'Key')
	{
		$html = "<select name=\"{$name}\">\n";
		$sql = "SELECT * FROM SNOM";
		$dbresult = mysql_query($sql) or die("Failed: $sql");
		while($dbrow = mysql_fetch_assoc($dbresult))
			{
		$html .= "<option>{$dbrow["Keyword"]}</option>\n";
		$html .= "</select>\n";
			}
		return $html;
	}
?>
